package com.users.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
 *     
 * 类名称：EncodeFilter    
 * 类描述：编码过滤器
 * 创建人：qjx    
 * 创建时间：2012-10-13 上午01:24:07    
 * @version v1.0
 *
 */
public class EncodeFilter implements Filter{
	protected String encoding = null;
	protected FilterConfig filterConfig = null;

	public void destroy() {
		this.encoding = null;
		this.filterConfig = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		String encoding = this.encoding;
		if (encoding != null) {
			request.setCharacterEncoding(encoding);
		} else {
			request.setCharacterEncoding("UTF-8");
		}
		chain.doFilter(request, response);// 传递过滤链
	}

	public void init(FilterConfig config) throws ServletException {
		this.filterConfig = config;
		this.encoding = filterConfig.getInitParameter("encoding");// 获取传递过来的初使化编码
	}
}
